package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.adapter.ConstructPlanOrContentAdapter;
import com.cloudsigns.factorymanager2.engineering.adapter.EditConstructOrderDispatchInformationAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructContentBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * 工程 工单管理 分派工单  工程施工 编辑派工信息
 * Created by tony on 2018/2/1.
 */

public class EditConstructOrderDispatchInfomationActivity extends Activity implements View.OnClickListener, ItemRemoveListener {
    private ImageView img_back;
    private TextView tv_title;
    private TextView btn_upload;
    private ListView lv;
    private ImageView img_edit_construct_content;
    private EditConstructOrderDispatchInformationAdapter adapter;
    private List<EngineeringConstructContentBean> datas = new ArrayList<>();
    private List<String> selectedItemList = new ArrayList<String>();
    private final String TAG = EditConstructOrderDispatchInfomationActivity.class.getSimpleName();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_edit_construct_order_dispatch_information);

        init();
    }

    private void init() {
        img_back = findViewById(R.id.img_back);
        tv_title = findViewById(R.id.tv_title);
        btn_upload = findViewById(R.id.btn_upload);
        img_edit_construct_content = findViewById(R.id.img_edit_construct_content);
        lv = findViewById(R.id.lv);


        tv_title.setText(getResources().getText(R.string.edit_dispatch_information));
        img_back.setOnClickListener(this);
        btn_upload.setOnClickListener(this);
        img_edit_construct_content.setOnClickListener(this);

        adapter = new EditConstructOrderDispatchInformationAdapter(this, TAG);
        adapter.setData(datas);
        adapter.setItemRemoveListener(this);
        lv.setAdapter(adapter);
    }

    private void upload() {
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);

        Toast.makeText(this, "上传成功！", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void remove(int positon) {
        if (positon >= 0 && positon < datas.size()) {
            datas.remove(positon);
            selectedItemList.remove(positon);
            adapter.setData(datas);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void remove(View view) {

    }


    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_upload:
                upload();
                break;
            case R.id.img_edit_construct_content:
                mIntent = new Intent(EditConstructOrderDispatchInfomationActivity.this, AddConstructContentForEngineeringDispatchActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, (Serializable) selectedItemList);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    selectedItemList = (List<String>) data.getSerializableExtra(ConstantUtil.KEY_1);
                    datas = (List<EngineeringConstructContentBean>) data.getSerializableExtra(ConstantUtil.KEY_2);

                    adapter.setData(datas);
                    adapter.notifyDataSetChanged();
                }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
